package com.ruicar.afs.cloud.afscase.monitor.sort;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruicar.afs.cloud.afscase.monitor.vo.PersonalMonitorVO;
import lombok.Builder;
import lombok.Data;

import java.util.*;


/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/09/26 20:38
 */
public class AgingMonitorSort {

    private static final String TYPE_APPROVE_STAFF = "TYPE_APPROVE_STAFF";
    private static final String TYPE_INDEX_OF_APPROVE_STAFF = "TYPE_INDEX_OF_APPROVE_STAFF";
    private static final String TYPE_APPROVE_LEADER = "TYPE_APPROVE_LEADER";
    private static final String TYPE_INDEX_OF_APPROVE_LEADER = "TYPE_INDEX_OF_APPROVE_LEADER";

    private static final Map<String,MonitorSortIndex> SORT_INDEX_MAP = new HashMap<>();

    public static String ROLE_SEPARATOR = ",";

    public static final Comparator<? super PersonalMonitorVO> COMPARATOR;

    static {
        SORT_INDEX_MAP.put(TYPE_APPROVE_STAFF, MonitorSortIndex.builder().roleName("审批员").index(1).build());
        SORT_INDEX_MAP.put(TYPE_INDEX_OF_APPROVE_STAFF, MonitorSortIndex.builder().roleName("包含审批员").index(2).build());
        SORT_INDEX_MAP.put(TYPE_APPROVE_LEADER, MonitorSortIndex.builder().roleName("审批组长").index(3).build());
        SORT_INDEX_MAP.put(TYPE_INDEX_OF_APPROVE_LEADER, MonitorSortIndex.builder().roleName("包含审批组长").index(4).build());
        COMPARATOR = Comparator.comparing(AgingMonitorSort::getIndex);
    }

    /**
    * @Description 指定角色放置首位显示
    * @Author Peng_Jinbo
    * @Date 2020/9/26 21:35
    */
    private static final void exchangeIndex(PersonalMonitorVO p1,String type){
        List<String> roleList = Arrays.asList(p1.getJobPosition().split(ROLE_SEPARATOR));
        String limbo = "";
        int limboIndex = -1;
        for (int i = 0; i < roleList.size(); i++) {
            if(SORT_INDEX_MAP.get(type).getRoleName().equals(roleList.get(i))){
                limbo = roleList.get(i);
                limboIndex = i;
                break;
            }
        }
        if(limboIndex > 0) {
            String ori = roleList.get(0);
            roleList.set(0,limbo);
            roleList.set(limboIndex,ori);
            p1.setJobPosition(CollectionUtil.join(roleList,ROLE_SEPARATOR));
        }
    }


    private static final Integer getIndex(PersonalMonitorVO p1) {
        if (StrUtil.isBlank(p1.getJobPosition())) return 0;
        String type;
        if (SORT_INDEX_MAP.containsKey(p1.getJobPosition())) {
            type = p1.getJobPosition();
        } else {
            if (StrUtil.equals(p1.getJobPosition(), SORT_INDEX_MAP.get(TYPE_APPROVE_STAFF).getRoleName())) {
                type = TYPE_APPROVE_STAFF;
            } else if (StrUtil.equals(p1.getJobPosition(), SORT_INDEX_MAP.get(TYPE_APPROVE_LEADER).getRoleName())) {
                type = TYPE_APPROVE_LEADER;
            } else if (p1.getJobPosition().indexOf(SORT_INDEX_MAP.get(TYPE_APPROVE_STAFF).getRoleName()) >= 0) {
                exchangeIndex(p1, TYPE_APPROVE_STAFF);
                type = TYPE_INDEX_OF_APPROVE_STAFF;
            } else if (p1.getJobPosition().indexOf(SORT_INDEX_MAP.get(TYPE_APPROVE_LEADER).getRoleName()) >= 0) {
                exchangeIndex(p1, TYPE_APPROVE_LEADER);
                type = TYPE_INDEX_OF_APPROVE_LEADER;
            } else {
                SORT_INDEX_MAP.put(p1.getJobPosition(),
                        MonitorSortIndex.builder()
                                .roleName(p1.getJobPosition())
                                .index(Math.abs(p1.getJobPosition().hashCode()) + SORT_INDEX_MAP.size())
                                .build());
                type = p1.getJobPosition();
            }
        }
        return SORT_INDEX_MAP.get(type).getIndex();
    }


    @Data
    @Builder
    private static class MonitorSortIndex{
        private String roleName;
        private Integer index;
    }

}
